package exttable.timetable;

import exttable.SGTTestParametrs;
import exttable.timetable.handler.TimeTableHandler;
import exttable.timetable.models.VisitModel;
import exttable.timetable.ttpanels.AnalysisFillerPanel;
import exttable.timetable.ttpanels.VisitFillerPanel;
import utils.Localisation;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Apr 11, 2010
 * Time: 3:04:30 PM
 * To change this template use File | Settings | File Templates.
 */
public class TimeTableMainPanel {
    public static final int VISIT_PANEL = 0;
    public static final int ANALYSIS_PANEL = 1;
    public static final int TIMETABLE_PANEL = 2;

    private JFrame frame;
    private boolean fileIsReady = false;
    private String schemaDescription;

    public int position = 0;
    private VisitModel visitModel;
    private VisitFillerPanel vfp;
    private AnalysisFillerPanel afp;

    private JPanel[] panelsArray = new JPanel[3];

    private AbstractButton backVT;
    private AbstractButton nextVT;
    private JPanel visitPanel;

    public TimeTableMainPanel() {
        backVT = new JButton(Localisation.getMessage("form.back"));
        nextVT = new JButton(Localisation.getMessage("form.continue"));
        vfp = new VisitFillerPanel();
        afp = new AnalysisFillerPanel();
        visitModel = new VisitModel();
        setPanelsArray(vfp.getVisitPanel(), VISIT_PANEL);
        visitPanel = new JPanel();
        createGUI();
    }

    void createGUI() {
        frame = new JFrame();
        visitPanel = getPanelsArray(VISIT_PANEL);

        nextVT.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                switch (position) {
                    case 0:
                        vfp.stopEditing();
                        getVisitModel().setSchemaVisits(vfp.validateTableData());
                        if (getVisitModel().getSchemaVisits() != null && getVisitModel().getSchemaVisits().size() > 0) {
                            frame.getContentPane().removeAll();
                            position++;
                            if (getPanelsArray(ANALYSIS_PANEL) == null) {
                                setPanelsArray(afp.initTabPanels(), ANALYSIS_PANEL);
                            }
                            visitPanel = getPanelsArray(ANALYSIS_PANEL);
                            organiseLayout();
                        }
                        break;
                    case 1:

                        frame.getContentPane().removeAll();
                        position++;
                        setPanelsArray(afp.analysisPanelValidator(getVisitModel().getSchemaVisits()), TIMETABLE_PANEL);
                        visitPanel = getPanelsArray(TIMETABLE_PANEL);
                        organiseLayout();
                        break;
                    case 2:
                        if (new TimeTableHandler().analysisCheckerValidator(getVisitModel().getSchemaVisits(), afp.getVisitAnalysisArray(), SGTTestParametrs.getInstance())) {
                            frame.dispose();
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        );

        backVT.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.getContentPane().remove(getPanelsArray(position));
                visitPanel = getPanelsArray(position - 1);
                position--;
                organiseLayout();
            }
        }
        );
        frame.setSize(800, 730);
        organiseLayout();
        frame.setTitle(Localisation.getMessage("title.timetable"));
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    private void organiseLayout() {
        frame.add(visitPanel);
        frame.add(nextVT);
        frame.add(getBackVT());
        SpringLayout layout = new SpringLayout();
        frame.setLayout(layout);

        layout.putConstraint(SpringLayout.WEST, visitPanel, 10, SpringLayout.NORTH, frame);
        layout.putConstraint(SpringLayout.NORTH, visitPanel, 0, SpringLayout.SOUTH, frame);

        layout.putConstraint(SpringLayout.WEST, backVT, 5, SpringLayout.WEST, visitPanel);
        layout.putConstraint(SpringLayout.NORTH, backVT, 5, SpringLayout.SOUTH, visitPanel);

        layout.putConstraint(SpringLayout.WEST, nextVT, 10, SpringLayout.EAST, backVT);
        layout.putConstraint(SpringLayout.NORTH, nextVT, 5, SpringLayout.SOUTH, visitPanel);
        frame.repaint();
        frame.validate();
    }


    public JPanel getPanelsArray(int i) {
        return panelsArray[i];
    }

    public void setPanelsArray(JPanel panel, int i) {
        panelsArray[i] = panel;
    }

    public boolean isFileIsReady() {
        return fileIsReady;
    }

    public void setFileIsReady(boolean fileIsReady) {
        this.fileIsReady = fileIsReady;
    }

    public String getSchemaDescription() {
        return schemaDescription;
    }

    public void setSchemaDescription(String schemaDescription) {
        this.schemaDescription = schemaDescription;
    }

    public AbstractButton getBackVT() {
        if (position == 0) {
            backVT.setEnabled(false);
            return backVT;
        } else {
            backVT.setEnabled(true);
            return backVT;
        }
    }

    public AbstractButton getNextVT() {
        return nextVT;
    }

    public void setBackVT(AbstractButton backVT) {
        this.backVT = backVT;
    }

    public VisitModel getVisitModel() {
        return visitModel;
    }

    public void setVisitModel(VisitModel visitModel) {
        this.visitModel = visitModel;
    }
}
